<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>免疫信息管理测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input, select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            background-color: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 10px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .btn-danger {
            background-color: #dc3545;
        }
        .btn-danger:hover {
            background-color: #c82333;
        }
        .btn-success {
            background-color: #28a745;
        }
        .btn-success:hover {
            background-color: #218838;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f8f9fa;
        }
        .result {
            margin-top: 20px;
            padding: 10px;
            border-radius: 4px;
        }
        .success {
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .error {
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
    </style>
</head>
<body>
    <h1>免疫信息管理测试</h1>

    <!-- 添加免疫信息 -->
    <div class="container">
        <h2>添加免疫信息</h2>
        <form id="addForm">
            <div class="form-group">
                <label for="rearingDays">饲养天数:</label>
                <input type="number" id="rearingDays" name="rearingDays" required>
            </div>
            <div class="form-group">
                <label for="way">免疫方式:</label>
                <input type="text" id="way" name="way" required>
            </div>
            <div class="form-group">
                <label for="function">免疫方法:</label>
                <input type="text" id="function" name="function" required>
            </div>
            <div class="form-group">
                <label for="productionBatch">生产批次:</label>
                <input type="text" id="productionBatch" name="productionBatch" required>
            </div>
            <button type="submit" class="btn-success">添加</button>
        </form>
    </div>

    <!-- 搜索和列表 -->
    <div class="container">
        <h2>免疫信息列表</h2>
        <div class="form-group">
            <label for="searchKeyword">搜索关键词:</label>
            <input type="text" id="searchKeyword" placeholder="输入免疫方式、方法或批次进行搜索">
        </div>
        <button onclick="loadList()" class="btn-success">搜索</button>
        <button onclick="loadList()" class="btn-success">刷新列表</button>
        
        <hr style="margin: 20px 0;">
        
        <h3>根据生产批次查询</h3>
        <div class="form-group">
            <label for="batchSearch">生产批次:</label>
            <input type="text" id="batchSearch" placeholder="输入生产批次进行精确查询">
        </div>
        <button onclick="loadByBatch()" class="btn-success">按批次查询</button>
        
        <table id="dataTable">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>饲养天数</th>
                    <th>免疫方式</th>
                    <th>免疫方法</th>
                    <th>生产批次</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody id="dataBody">
            </tbody>
        </table>
    </div>

    <!-- 编辑免疫信息 -->
    <div class="container" id="editContainer" style="display: none;">
        <h2>编辑免疫信息</h2>
        <form id="editForm">
            <input type="hidden" id="editId" name="id">
            <div class="form-group">
                <label for="editRearingDays">饲养天数:</label>
                <input type="number" id="editRearingDays" name="rearingDays" required>
            </div>
            <div class="form-group">
                <label for="editWay">免疫方式:</label>
                <input type="text" id="editWay" name="way" required>
            </div>
            <div class="form-group">
                <label for="editFunction">免疫方法:</label>
                <input type="text" id="editFunction" name="function" required>
            </div>
            <div class="form-group">
                <label for="editProductionBatch">生产批次:</label>
                <input type="text" id="editProductionBatch" name="productionBatch" required>
            </div>
            <button type="submit" class="btn-success">更新</button>
            <button type="button" onclick="cancelEdit()">取消</button>
        </form>
    </div>

    <div id="result"></div>

    <script>
        const API_BASE = '/api/ImmunityInfo';

        // 显示结果
        function showResult(message, isSuccess = true) {
            const resultDiv = document.getElementById('result');
            resultDiv.innerHTML = `<div class="result ${isSuccess ? 'success' : 'error'}">${message}</div>`;
            setTimeout(() => {
                resultDiv.innerHTML = '';
            }, 3000);
        }

        // 添加免疫信息
        document.getElementById('addForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            const formData = new FormData(e.target);
            const data = {
                rearingDays: parseInt(formData.get('rearingDays')),
                way: formData.get('way'),
                function: formData.get('function'),
                productionBatch: formData.get('productionBatch')
            };

            try {
                const response = await fetch(API_BASE, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(data)
                });

                const result = await response.json();
                if (result.success) {
                    showResult('添加成功: ' + result.message);
                    e.target.reset();
                    loadList();
                } else {
                    showResult('添加失败: ' + result.message, false);
                }
            } catch (error) {
                showResult('请求失败: ' + error.message, false);
            }
        });

        // 加载列表
        async function loadList() {
            const searchKeyword = document.getElementById('searchKeyword').value;
            const url = `${API_BASE}?searchKeyword=${encodeURIComponent(searchKeyword)}`;

            try {
                const response = await fetch(url);
                const result = await response.json();
                
                if (result.success) {
                    displayData(result.data.items);
                } else {
                    showResult('加载失败: ' + result.message, false);
                }
            } catch (error) {
                showResult('请求失败: ' + error.message, false);
            }
        }

        // 显示数据
        function displayData(items) {
            const tbody = document.getElementById('dataBody');
            tbody.innerHTML = '';

            items.forEach(item => {
                const row = document.createElement('tr');
                row.innerHTML = `
                    <td>${item.id}</td>
                    <td>${item.rearingDays}</td>
                    <td>${item.way}</td>
                    <td>${item.function}</td>
                    <td>${item.productionBatch}</td>
                    <td>
                        <button onclick="editItem(${item.id})" class="btn-success">编辑</button>
                        <button onclick="deleteItem(${item.id})" class="btn-danger">删除</button>
                    </td>
                `;
                tbody.appendChild(row);
            });
        }

        // 删除项目
        async function deleteItem(id) {
            if (!confirm('确定要删除这条记录吗？')) {
                return;
            }

            try {
                const response = await fetch(`${API_BASE}/${id}`, {
                    method: 'DELETE'
                });

                const result = await response.json();
                if (result.success) {
                    showResult('删除成功: ' + result.message);
                    loadList();
                } else {
                    showResult('删除失败: ' + result.message, false);
                }
            } catch (error) {
                showResult('请求失败: ' + error.message, false);
            }
        }

        // 根据生产批次查询
        async function loadByBatch() {
            const batchKeyword = document.getElementById('batchSearch').value;
            if (!batchKeyword.trim()) {
                showResult('请输入生产批次', false);
                return;
            }

            try {
                const response = await fetch(`${API_BASE}/by-batch?productionBatch=${encodeURIComponent(batchKeyword)}`);
                const result = await response.json();
                
                if (result.success) {
                    displayData(result.data);
                    showResult(result.message);
                } else {
                    showResult('查询失败: ' + result.message, false);
                }
            } catch (error) {
                showResult('请求失败: ' + error.message, false);
            }
        }

        // 编辑项目
        async function editItem(id) {
            // 从当前列表中查找数据
            const items = Array.from(document.querySelectorAll('#dataBody tr')).map(row => {
                const cells = row.querySelectorAll('td');
                return {
                    id: parseInt(cells[0].textContent),
                    rearingDays: parseInt(cells[1].textContent),
                    way: cells[2].textContent,
                    function: cells[3].textContent,
                    productionBatch: cells[4].textContent
                };
            });
            
            const item = items.find(item => item.id === id);
            if (!item) {
                showResult('未找到要编辑的数据', false);
                return;
            }

            // 填充编辑表单
            document.getElementById('editId').value = item.id;
            document.getElementById('editRearingDays').value = item.rearingDays;
            document.getElementById('editWay').value = item.way;
            document.getElementById('editFunction').value = item.function;
            document.getElementById('editProductionBatch').value = item.productionBatch;

            // 显示编辑表单
            document.getElementById('editContainer').style.display = 'block';
        }

        // 取消编辑
        function cancelEdit() {
            document.getElementById('editContainer').style.display = 'none';
            document.getElementById('editForm').reset();
        }

        // 编辑表单提交
        document.getElementById('editForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            const formData = new FormData(e.target);
            const data = {
                id: parseInt(formData.get('id')),
                rearingDays: parseInt(formData.get('rearingDays')),
                way: formData.get('way'),
                function: formData.get('function'),
                productionBatch: formData.get('productionBatch')
            };

            try {
                const response = await fetch(API_BASE, {
                    method: 'PUT',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(data)
                });

                const result = await response.json();
                if (result.success) {
                    showResult('更新成功: ' + result.message);
                    cancelEdit();
                    loadList();
                } else {
                    showResult('更新失败: ' + result.message, false);
                }
            } catch (error) {
                showResult('请求失败: ' + error.message, false);
            }
        });

        // 页面加载时获取列表
        window.onload = function() {
            loadList();
        };
    </script>
</body>
</html> 